﻿using ConfExpert.Datasets;
using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevTeam;

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Aksesuar
{
    public partial class Aks1 : XForm
    {
        public Aks1()
        {
            InitializeComponent();
        }

        private void Aks1_Load(object sender, EventArgs e) 
        {
            

            gleKategori.Edit.Assign(RI(Rp.ProductCategory));
            gleKategori.EditValueChanged += gleKategori_EditValueChanged;
            bs = gridView1.Init("GetAksByCategory", 0);
            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;
            gridView1.RowCellStyle += gridView1_RowCellStyle;

            gridView1.MakeColumnInvisible("Lokasyon", "ReorderPoint", "Usable", "Ekleyen", "Guncelleyen");
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gridView1.BestFitColumns();

            //GridManager gm = GridManager.GetManager(gridView1);
            //gm.GridMenu.AddMenu("Ekle", Ekle);

            barButtonItem1.ItemClick += barButtonItem1_ItemClick;
        }

        void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            gridView1.Update("", "Aksesuar");
            gridView1.BestFitColumns();
            Notify("Saved");
        }

        //DataSet d;
        int KategoriId = 0;
        BindingSource bs;
        void gleKategori_EditValueChanged(object sender, EventArgs e)
        {
            gridView1.EndEditing();

            gridView1.RefreshDataSource("GetAksByCategory", gleKategori.EditValue.ToInt());
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            gridView1.BestFitColumns();
            KategoriChanged();

            /*
            d = ExecDataSet("GetAksJoker", KategoriId, 0);
            for (int i = 0; i < d.Tables.Count; i++)
            {                
                //RepositoryItemComboBox ri = new RepositoryItemComboBox();
                //foreach (DataRow item in d.Tables[i].Rows)
                //    ri.Items.Add(item["Name"]);               

                RepositoryItemGridLookUpEdit ri = new RepositoryItemGridLookUpEdit();
                ri.TextEditStyle = TextEditStyles.DisableTextEditor;
                ri.Assign(GetIntellisensedRI(d.Tables[i]));
                ri.ProcessNewValue += new ProcessNewValueEventHandler(ri_ProcessNewValue2);

                ri.BestFitMode = BestFitMode.BestFitResizePopup;
                ri.PopupFormMinSize = new Size(0, 0);

                gridView1.Columns["A" + (i + 1).ToString()].ColumnEdit = ri;
            }
            */
        }


        private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                if (e.Column.FieldName.Length == 2 &&
                    e.Column.FieldName.StartsWith("A"))
                {
                    DataRow dr = gridView1.GetDataRow(e.RowHandle);
                    int D = intParse(dr["ProductCategoryId"]);

                    e.Appearance.ForeColor = (KategoriId != D ? Color.LightGray : Color.Black);
                }
            }
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gridView1.FocusedRowHandle > -1)
                KategoriChanged();
        }


        private void KategoriChanged()
        {
            /*
            try
            {
                gridView1.CloseEditor();
                gridView1.BeginUpdate();
                if (KategoriId != gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ProductCategoryId").ToInt(-1))
                {
                    KategoriId = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ProductCategoryId").ToInt(-1);

                    MyData.ProductCategoryRow r = AppSession.Lookups.ProductCategory.FindByProductCategoryId(KategoriId);
                    if (r != null)
                        for (int i = 1; i < 6; i++)
                        {
                            string fieldName = "A" + i.ToString();
                            string cap = r[fieldName].ToString();
                            gridView1.Columns[fieldName].Caption = cap != string.Empty ? cap : " ";
                        }

                    //gridView1.BestFitColumns();
                }            
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
                gridView1.EndUpdate();
            }
             */
        }

    }
}
